Organizing photos and videos using linux commands

linux
Published

June 10, 2025

I have a nextcloud instance to backup personal data, and on my phone, I use the auto upload feature of the nextcloud app to save automatically all the pictures and videos I take. Accross, I had different smartphones models, which use different names to store photos and videos (like DCIM, Camera, Camera_100, etc.).

Therefore, after several years of auto uploading, this folder is a mess. Here are the commands I used to organize it (I put it here also for the next time). The strategy was to copy all the files to the root folder, and then organize it in a year / month folder structure. In all the mess, there are duplicates (problem #1) but also photos with the same name that are not duplicates (problem #2, for example IMG_1234.jpg).

In a linux terminal, I followed the following steps:

  1. Add md5 checksum to each filename, so problem #2 is solved:

    find -type f -print0 | while IFS= read -r -d '' f; do md=($(md5sum "${f}")); filename="${f%.*}"; ext="${f##*.}"; mv "$f" "$filename-$md.$ext"; done

  2. Move all the files to the root folder (problem #1 is solved because duplicates should have the same name and same checksum, with “-n” parameter I indicate to mv to not copy those):

    find . -type f -mindepth 2 -exec mv -i -n -- {} . \;

  3. Rename all the files to follow the pattern YY-MM-DD_HH-MM-SS using EXIF information. I save the log to facilitate a later exploration:

    exiftool '-filename<CreateDate' -d %y-%m-%d_%H-%M-%S-%%-c.%%le -ext jpg -ext jpeg -ext mp4 -r . 2>&1 | tee res.log

  4. Here I had pictures with missing EXIF information, so they could not be renamed. A lot of them were started with YYYYMMDD_, so I renamed them with:

    for f in $(ls); do mv "$f" "${f:2:2}-${f:4:2}-${f:6:999}"; done

  5. Finally, to have everything organized in folders with years / months, I used:

    for f in $(ls); do mkdir -p "20${f:0:2}/${f:3:2}"; mv "$f" "20${f:0:2}/${f:3:2}/$f"; done

  6. Some photos remained unclassified due to a lack of EXIF information and a weird filename. I had to classified them manually.

I hope this information will be useful for others.